package com.rdgzs.framework.utilsTools;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springside.modules.orm.Page;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.rdgzs.framework.constant.SysConstants;

/**
 * ###################################################
 * 创建日期：2012－12－21
 * 开发人员：李广彬
 * 功能描述：html 特殊字符转码
 * 修改日志：
 * ###################################################
 */
public class HTMLUtils {
	private static final Log logger = LogFactory.getLog(HTMLUtils.class);
	private static final Pattern HtmlTagPattern = Pattern.compile("<(\\S*?)[^>]*>.*?|<.*?/>");
	private static final Map<String, String> HTML_SEPARATOR = new HashMap() {
		{
			put("\"", "&quot;");
			put("<", "&lt;");
			put(">", "&gt;");
			put("‘", "&lsquo;");
			put("’", "&rsquo;");
			put("“", "&ldquo;");
			put("”", "&rdquo;");
			put("(", "&#40;");
			put(")", "&#41;");
			put(",", "&#44;");
			put("=", "&#61;");
			put("*", "&#42;");
			put("\'", "&#39;");
			put("\\", "&#92;");
			put("^", "&#94;");
			put(" ", "&nbsp;");
			put("--", "&oline;&oline;");
			put("\n", "<br/>");
		}
	};
	
	/**
	 * 替换整个字符串内的特殊字符
	 * @param htmlStr
	 * @return
	 */
	public static String convertHTML(final String htmlStr,Map HTMLSeparatorExt) {
		StringBuffer convertBuf = new StringBuffer();
		String htmlSeparator;
		Map<String, String> HTMLSeparator=new HashMap();
		HTMLSeparator.putAll(HTML_SEPARATOR);
		if(HTMLSeparatorExt!=null){
			HTMLSeparator.putAll(HTMLSeparatorExt);
		}
		if(htmlStr!=null){
			char[] chars = htmlStr.toCharArray();
			for(char htmlChar:chars){
				htmlSeparator=HTMLSeparator.get(String.valueOf(htmlChar));
				if(htmlSeparator==null){
					convertBuf.append(htmlChar);
				}else{
					convertBuf.append(htmlSeparator);
				}
			}
		}
		return convertBuf.toString();
	}
	
	/**
	 * 直替换html内容区的特殊字符
	 * @param str
	 * @return
	 */
	public static String encode(final String htmlStr) {
		return encode(htmlStr,null);
	}
	
	/**
	 * 直替换html内容区的特殊字符
	 * @param str
	 * @return
	 */
	public static String encode(final String htmlStr,Map HTMLSeparatorExt) {
		StringBuffer convertBuf = new StringBuffer();
		Matcher m = HtmlTagPattern.matcher(htmlStr);
		int startInd = 0;
		int strLen = 0;
		int preEndInd = 0;
		if (m.find()) {
			startInd = m.start();
			if(startInd!=0){
				convertBuf.append(convertHTML(htmlStr.substring(0, startInd),HTMLSeparatorExt));
			}
			strLen = m.group().length();
			preEndInd = startInd + strLen;
			convertBuf.append(m.group());
		}else{
			convertBuf.append(convertHTML(htmlStr,HTMLSeparatorExt));
		}
		while (m.find()) {
			startInd = m.start();
			strLen = m.group().length();
			System.out.println(htmlStr.substring(preEndInd, startInd)+startInd+" "+preEndInd);
			convertBuf.append(convertHTML(htmlStr.substring(preEndInd, startInd),HTMLSeparatorExt));
			convertBuf.append(m.group());
			preEndInd = startInd + strLen;
		}
		if(preEndInd!=htmlStr.length()){
			convertBuf.append(convertHTML(htmlStr.substring(preEndInd, htmlStr.length()),HTMLSeparatorExt));
		}
		System.out.println(convertBuf.toString());
		logger.debug(convertBuf.toString());
		return convertBuf.toString();
	}
	
	
	public static void main(String args[]) throws NoSuchAlgorithmException {
		Map kk = new HashMap();
		kk.put("q", "q");
		kk.put("a", "a"); 
		
		//HTML_SEPARATOR={q=q};
		//String html = "\naa0<b>\n99\n0</b>（））(a<td id='kk'><input type='botton'/><b>990</b>橱架()圆顶地<a target=\"_blank\" href=\"http://www.baidu.com/baidu?cl=3&tn=baidutop10&fr=top1000&wd=%A1%B6%CC%C6%C9%BD%B4%F3%B5%D8%D5%F0%A1%B7%B9%AB%D3%B3\">《唐山大地震》公映</a></td>";
		String html = "\naa0<b>";
		String dd=encode(html);
		String kkk="1234567";
		System.out.print(html.length());
	}
}
